package com.kyhd.audio.service;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baidu.aip.speech.AipSpeech;
import com.baidu.aip.speech.TtsResponse;
import com.kyhd.audio.util.AppConfig;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class SpeechService {
	@Autowired
	AppConfig appConfig;

	public byte[] synthesis(String content) {
//		log.info("BaiduAppid:{},BaiduApikey:{},BaiduSecretkey:{}",appConfig.getBaiduAppid(), appConfig.getBaiduApikey(), appConfig.getBaiduSecretkey());
//		log.info("content:{}",content);
		// 初始化一个AipSpeech
      AipSpeech client = new AipSpeech(appConfig.getBaiduAppid(), appConfig.getBaiduApikey(), appConfig.getBaiduSecretkey());
//      AipSpeech client = new AipSpeech(BaiduAppid, BaiduApikey, BaiduSecretkey);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);

        // 可选：设置代理服务器地址, http和socket二选一，或者均不设置
//        client.setHttpProxy("proxy_host", proxy_port);  // 设置http代理
//        client.setSocketProxy("proxy_host", proxy_port);  // 设置socket代理

        // 可选：设置log4j日志输出格式，若不设置，则使用默认配置
        // 也可以直接通过jvm启动参数设置此环境变量
//        System.setProperty("aip.log4j.conf", "path/to/your/log4j.properties");

        // 调用接口
    	HashMap<String, Object> options = new HashMap<String, Object>();
        options.put("spd", appConfig.getBaiduSpd());
        options.put("pit", appConfig.getBaiduPit());
        options.put("per", appConfig.getBaiduPer());
        TtsResponse res = client.synthesis(content, "zh", 1, options);
        byte[] data = res.getData();
        if(data != null) {
        	return data;
        }else {
        	log.info("baidu speech api error:{}",res.getResult());
        	return null;
        }
	}
}